<template>
  <div class="strategy-like-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-left">
          <h2>
            <i class="icon-edit">📝</i>
            我发布的攻略
          </h2>
          <div class="header-subtitle">记录并分享你的旅行故事</div>
        </div>
        <div class="total-count">
          <span class="count-number">{{ strategyList.length }}</span>
          <span class="count-text">篇攻略</span>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <span class="loading-text">加载精彩内容中...</span>
    </div>

    <!-- 空状态 -->
    <div v-else-if="strategyList.length === 0" class="empty-state">
      <div class="empty-icon">
        <i class="icon-empty">💭</i>
      </div>
      <h3 class="empty-title">还没有发布的攻略</h3>
      <p class="empty-description">快来分享你的旅行经验和心得吧！</p>
      <el-button type="primary" class="explore-btn" @click="goToPublish">
        <i class="icon-explore">✍️</i>
        发布攻略
      </el-button>
    </div>

    <!-- 攻略列表 -->
    <div v-else class="strategy-list">
      <div v-for="strategy in pagedStrategies" :key="strategy.strategyId" class="strategy-card">
        <!-- 整个卡片可点击区域 -->
        <div class="card-clickable-area" @click="goToStrategyDetail(strategy.strategyId)">
          <!-- 图片容器 -->
          <div class="strategy-img-container">
            <img :src="strategy.coverImage" class="strategy-img" alt="攻略图片" />
            <div class="image-overlay"></div>
            <div class="edit-badge">
              <i class="badge-icon">📝</i>
            </div>
          </div>
          
          <!-- 内容区域 -->
          <div class="strategy-content">
            <h3 class="strategy-title">{{ strategy.strategyName || '未命名攻略' }}</h3>
            <p class="strategy-description">{{ extractTextContent(strategy.strategyContent).substring(0, 80) }}...</p>
            
            <!-- 元信息 -->
            <div class="strategy-meta">
              <div class="meta-item">
                <i class="meta-icon">📍</i>
                <span>{{ strategy.scenicAreaName || '未知景点' }}</span>
              </div>
              <div class="meta-item">
                <i class="meta-icon">📅</i>
                <span>{{ formatTime(strategy.modificationTime) }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 底部操作区 -->
        <div class="strategy-footer">
          <!-- 地区信息 -->
          <div class="region-info">
            <span class="region-text">{{ strategy.province }} - {{ strategy.city }}</span>
          </div>
          
          <!-- 操作按钮 -->
          <div class="strategy-actions">
            <button class="edit-btn" @click.stop="editStrategy(strategy.strategyId)">
              <i class="edit-icon">✏️</i>
              <span>编辑</span>
            </button>
            <button class="delete-btn" @click.stop="deleteStrategy(strategy.strategyId)">
              <i class="delete-icon">🗑️</i>
              <span>删除</span>
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页器 -->
    <div v-if="strategyList.length > 0" class="pagination-container">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="strategyList.length"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const strategyList = ref([])
const loading = ref(true)
const deletingId = ref(null)
const currentPage = ref(1)
const pageSize = 8

const pagedStrategies = computed(() => {
  const start = (currentPage.value - 1) * pageSize
  return strategyList.value.slice(start, start + pageSize)
})

const extractTextContent = (htmlContent) => {
  if (!htmlContent) return ''
  const div = document.createElement('div')
  div.innerHTML = htmlContent
  return div.textContent || div.innerText || ''
}

const handlePageChange = (page) => {
  currentPage.value = page
}

const formatTime = (time) => {
  if (!time) return ''
  return time.split('T')[0]
}

const fetchStrategyList = async () => {
  loading.value = true
  try {
    const userId = localStorage.getItem('userId')
    if (!userId) {
      ElMessage.warning('请先登录')
      router.push('/login')
      return
    }

    const response = await axios.get('http://localhost:8080/ts/listByUser', {
      params: { userId }
    })
    strategyList.value = response.data
  } catch (error) {
    ElMessage.error('获取攻略失败，请稍后重试')
    console.error(error)
  } finally {
    loading.value = false
  }
}

const deleteStrategy = async (id) => {
  try {
    await ElMessageBox.confirm('确定要删除该攻略吗？', '删除确认', {
      type: 'warning',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
    })

    deletingId.value = id
    const res = await axios.delete(`http://localhost:8080/ts/delete/${id}`)
    if (res.data.success) {
      ElMessage.success(res.data.message || '删除成功')
      fetchStrategyList()
    } else {
      ElMessage.error(res.data.message || '删除失败')
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除出错')
      console.error(error)
    }
  } finally {
    deletingId.value = null
  }
}

const editStrategy = (id) => {
  router.push(`/userframe/strategy/edit/${id}`)
}

const goToPublish = () => {
  router.push('/userframe/strategy/new')
}

const goToStrategyDetail = (id) => {
  router.push(`/homepage/strategy/detail/${id}`)
}

onMounted(() => {
  fetchStrategyList()
})
</script>

<style scoped>
.strategy-like-container {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
}

/* 页面头部样式 */
.page-header {
  margin-bottom: 32px;
}

.header-content {
  background: white;
  padding: 24px 32px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left h2 {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 8px 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.icon-edit {
  font-size: 24px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.header-subtitle {
  font-size: 14px;
  color: #7f8c8d;
  margin: 0;
}

.total-count {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 16px 20px;
  border-radius: 12px;
  min-width: 80px;
}

.count-number {
  font-size: 24px;
  font-weight: 700;
  line-height: 1;
}

.count-text {
  font-size: 12px;
  opacity: 0.9;
  margin-top: 4px;
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 300px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid #f3f4f6;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  font-size: 16px;
  color: #6b7280;
  font-weight: 500;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: white;
  border-radius: 16px;
  padding: 60px 40px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 24px;
  opacity: 0.6;
}

.empty-title {
  font-size: 20px;
  font-weight: 600;
  color: #374151;
  margin: 0 0 12px 0;
}

.empty-description {
  font-size: 16px;
  color: #6b7280;
  margin: 0 0 32px 0;
  line-height: 1.5;
}

.explore-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
  border: none;
  cursor: pointer;
  font-size: 16px;
}

.explore-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

/* 攻略列表 */
.strategy-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
}

.strategy-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
}

.strategy-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

.card-clickable-area {
  cursor: pointer;
}

.strategy-img-container {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.strategy-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.strategy-card:hover .strategy-img {
  transform: scale(1.1);
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.3) 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.strategy-card:hover .image-overlay {
  opacity: 1;
}

.edit-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 6px 12px;
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #3498db;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.badge-icon {
  font-size: 12px;
  animation: heartbeat 1.5s ease-in-out infinite;
}

@keyframes heartbeat {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); }
}

.strategy-content {
  padding: 20px;
}

.strategy-title {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 12px 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.strategy-description {
  font-size: 14px;
  color: #6b7280;
  line-height: 1.6;
  margin: 0 0 16px 0;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.strategy-meta {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #9ca3af;
}

.meta-icon {
  font-size: 14px;
}

.strategy-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: #f8fafc;
  border-top: 1px solid #e2e8f0;
}

.region-info {
  font-size: 14px;
  color: #4b5563;
}

.strategy-actions {
  display: flex;
  gap: 8px;
}

.edit-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: linear-gradient(135deg, #3498db 0%, #2c7be5 100%);
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 10px rgba(44, 123, 229, 0.3);
}

.edit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(44, 123, 229, 0.5);
}

.delete-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 10px rgba(192, 57, 43, 0.3);
}

.delete-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(192, 57, 43, 0.5);
}

.edit-icon, .delete-icon {
  font-size: 14px;
  animation: like-pulse 0.6s ease-in-out;
}

@keyframes like-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.3); }
}

/* 分页器 */
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 40px;
  padding: 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .strategy-like-container {
    padding: 16px;
  }
  
  .header-content {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }
  
  .strategy-list {
    grid-template-columns: 1fr;
  }
  
  .strategy-footer {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
}

@media (max-width: 480px) {
  .header-content {
    padding: 20px;
  }
  
  .header-left h2 {
    font-size: 24px;
  }
  
  .strategy-content {
    padding: 16px;
  }
  
  .strategy-actions {
    width: 100%;
    justify-content: space-between;
  }
  
  .edit-btn, .delete-btn {
    flex: 1;
    justify-content: center;
  }
}
</style>